const { Service } = require('egg');

class CertificateConfigService extends Service {

  async id(id) {
    return await this.ctx.model.CertificateConfig.findByPk(id, {
      raw: true,
    });
  }

  async list() {
    return await this.app.model.CertificateConfig.findAll({
      raw: true,
    });
  }

  async create(data) {
    const { certificateName, certificateRules } = data;

    return await this.app.model.CertificateConfig.create({
      certificateName,
      certificateRules,
      
    });
  }

  async delete(id) {
    return await this.app.model.CertificateConfig.destroy({
      where: { id },
    });
  }

  async update(data) {
    const { id, certificateName, certificateRules } = data;

    const configInfo = await this.app.model.CertificateConfig.findOne({
      where: { id },
    });
    if (configInfo) {
      const params = {};
      if (certificateName) {
        params.certificateName = certificateName;
      }

      if (certificateRules) {
        params.certificateRules = certificateRules;
      }

      const [updatedCount] = await this.app.model.CertificateConfig.update(params, {
        where: { id: id },
      });
      return updatedCount > 0 ? configInfo : null;
    }
    return null;
  }


}

module.exports = CertificateConfigService;